package com.engr.lyx.auth.biz.controller;

import com.engr.lyx.auth.biz.service.UserService;
import com.engr.lyx.base.client.req.UserReq;
import com.engr.lyx.common.base.ResultBody;
import com.engr.lyx.common.security.UserHelper;
import com.engr.lyx.common.security.UserProfileDetails;
import com.engr.lyx.common.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenStore tokenStore;

    @GetMapping(value = "/users/current", name = "获取SSO当前用户信息")
    public ResultBody<UserProfileDetails> getUserAuth() {

        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        return ResultUtils.success(userProfileDetails);
    }

    @PostMapping(value = "/login/user", name = "获取SSO当前用户信息")
    public ResultBody getToken(@RequestBody UserReq userReq) {

        return ResultUtils.success(userService.login(userReq));
    }

    @GetMapping(value = "/logout/user", name = "退出登录")
    public ResultBody logout(@RequestParam String token) {

        tokenStore.removeAccessToken(tokenStore.readAccessToken(token));
        return ResultUtils.success("");
    }
}
